#include "gui/button.h"
#include "gui/defaultdefines.h"
#include "gui/guimessage.h"

#include "matrixstack/matrixstack.h"

#include "renderer/renderer.h"

#include "gamestate.h"
#include "resourcemanager.h"

Button::Button(std::string name, GLuint tex, std::unique_ptr<GUIMessage> message):Widget(){
    x = 0;
    y = 0;
    z = 1;
    width = DEFAULT_BUTTON_WIDTH;
    height = DEFAULT_BUTTON_WIDTH;
    this->name = name;
    selected = false;
    texture = tex;
    clickedMessage = std::move(message);
    obscured = 0;
    BuildVBO();
}

void Button::PreDraw(){
    Widget::PreDraw();

    if(obscurorUniform == 0){
        GLint program;
        glGetIntegerv(GL_CURRENT_PROGRAM, &program);
        obscurorUniform = glGetUniformLocation(program, "obscuror");
        if(obscurorUniform == -1){
            std::cout<<"Error locating obscuror uniform\n";
        }
    }

    if(clickedMessage){
        Entity* entity = gamestate->GetEntity(clickedMessage->i);
        if(entity){
            glUniform1i(obscurorUniform, 1);
        }
        else{
            glUniform1i(obscurorUniform, 0);
        }
    }
}

void Button::PostDraw(){
    Font* font = renderer->GetFont();
    font->Write(name, guiStack->Getx() + 16, guiStack->Gety() - 20, 20);
    Widget::PostDraw();
}
